package canChange;

import java.util.Arrays;
import java.util.Comparator;

public class Solution {
    public boolean canChange(String start, String target) {
        // 从target最右边开始
        if(getCount(start,'R')!=getCount(target,'R')) {
            return false;
        }
        if(getCount(start,'L')!=getCount(target,'L')) {
            return false;
        }
        int n = target.length();
        int i = n-1;
        int j = n-1;
        while (i>=0&&j>=0) {
            while (i>=0&&target.charAt(i)=='_'){
                i--;
            }
            while (j>=0&&start.charAt(j)=='_'){
                j--;
            }
            if(i<0||j<0) {
                break;
            }
            if(target.charAt(i)=='R'&&start.charAt(j)=='R'){
                if(i<j){
                    return false;
                }
            }else if(target.charAt(i)=='L'&&start.charAt(j)=='L'){
                if(i>j){
                    return false;
                }
            }else {
                return false;
            }
        }
        return true;
    }
    private int getCount(String s,char c) {
        int count = 0;
        for (int i = 0; i < s.length(); i++) {
            if(s.charAt(i) == c) {
                count++;
            }
        }
        return count;
    }

    public static void main(String[] args) {
        int[] arr = new int[]{1,0,7,6,5,4,9,3};
        int[] index = new int[]{0,4,3,1,2,0,5,4,3,6,7};
        String tel = "";
        for (int i: index) {
            tel+=arr[i];
        }
        System.out.println(tel);
    }
}
